<template>
  <div class="musiclist">
    <div class="musiclist-header">
      <div class="musiclist-title" v-text="musicListTitle"></div>
      <div class="musiclist-controller">
        <div class="musiclist-controller-btn" v-if="musicList.length !== 0" @click="playAllMusic">
          <i class="fa fa-play fa-fw"></i>播放全部</div>
        <div class="musiclist-controller-btn" v-if="musicList.length !== 0 && isMusicListNow && accountStatus" @click="changeEditStatus">
          <span v-if="!editing">
            <i class="fa fa-floppy-o fa-fw"></i>保存为歌单</span>
          <span v-if="editing">
            <i class="fa fa-arrow-left fa-fw"></i>退出编辑模式</span>
        </div>
      </div>
    </div>
    <div class="musiclist-intro" v-if="!isMusicListNow && musicMenu.intro && !editing" v-text="musicMenu.intro"></div>
    <div class="musiclist-content">
      <div class="musiclist-empty" v-if="musicList.length === 0">列表没有歌曲</div>
      <div class="musiclist-edit" v-if="editing">
        <div class="form-group">
          <input type="text" id="menuname" placeholder="歌单名称" v-model="editInfo.menuname">
          <label for="menuname">
            <i class="fa fa-th-list"></i>
          </label>
        </div>
        <div class="form-group">
          <input type="text" id="menuinfo" placeholder="歌单简介" v-model="editInfo.intro">
          <label for="menuinfo">
            <i class="fa fa-bookmark-o"></i>
          </label>
        </div>
      </div>
      <ul>
        <li v-for="(item, index) in musicList" class="musiclist-li">
          <div class="li-info musiclist-edit">
            <router-link :to="{name:'MusicInfo', params: {type: getType(item.type),id: item.songId}}">
              <span class="li-title" v-text="item.songName"></span>
            </router-link>
            <span class="li-singer" v-if="item.singer" v-text="item.singer"></span>
            <span class="li-type" v-if="item.type">（
              <span v-text="getTypeString(item.type)"></span>）</span>
            <div class="form-group" v-if="editing">
              <input type="text" placeholder="歌曲介绍" v-model="item.intro">
              <label>
                <i class="fa fa-flag-o"></i>
              </label>
            </div>
            <div class="li-intro" v-if="item.intro && !editing" v-text="item.intro"></div>
          </div>
          <div class="li-controller">
            <div class="li-controller-btn" @click="playNewMusic(item.songId, getType(item.type))">
              <i class="fa fa-play fa-fw"></i>播放</div>
            <div class="li-controller-btn" v-if="isMusicListNow" @click="removeMusicList(index)">
              <i class="fa fa-trash fa-fw"></i>删除</div>
          </div>
        </li>
      </ul>
      <div class="create-hint" v-if="error" v-text="hint"></div>
      <div class="create-btn" v-if="editing" @click="createMenu">创建歌单</div>
    </div>
  </div>
</template>
<script>
import { mapState } from 'vuex';

import APIURL from '../APIURL';

export default {
  name: 'musiclist',
  data() {
    return {
      musicMenu: {
        menuname: '...',
        intro: '...',
        songs: [],
      },
      connect: false,
      status: true,
      editing: false,
      error: false,
      hint: '',
      editInfo: {
        menuname: '',
        intro: '',
        songs: [],
      },
    };
  },
  computed: {
    ...mapState({
      id: state => state.musicplayer.id,
      uid: state => state.account.uid,
      token: state => state.account.token,
      mpMusicList: state => state.musicplayer.musicList,
      accountStatus: state => state.account.status,
    }),
    isMusicListNow() { return this.$route.name === 'MusicListNow'; },
    musicList() {
      if (this.isMusicListNow) {
        if (this.editing) {
          return this.editInfo.songs;
        }
        return this.mpMusicList;
      }
      return this.musicMenu.songs;
    },
    musicListTitle() {
      if (this.isMusicListNow) {
        return '我的播放列表';
      }
      return this.musicMenu.menuname;
    },
  },
  watch: {
    $route() {
      this.init();
    },
  },
  methods: {
    getType(typeNum) {
      switch (typeNum) {
        case 1:
          return 'yc';
        case 2:
          return 'fc';
        case 3:
          return 'bz';
        default:
          return '';
      }
    },
    getTypeString(typeNum) {
      switch (typeNum) {
        case 1:
          return '原创';
        case 2:
          return '翻唱';
        case 3:
          return '伴奏';
        default:
          return '';
      }
    },
    showHint(text) {
      this.hint = text;
      this.error = true;
    },
    hideHint() {
      this.error = false;
      this.hint = '';
    },
    changeEditStatus() {
      if (!this.editing) {
        const temp = JSON.stringify(this.musicList);
        this.editInfo.songs = JSON.parse(temp);
      }
      this.editing = !this.editing;
      this.hideHint();
    },
    playNewMusic(id, type) {
      this.$store.dispatch('getMusic', { id, type });
    },
    playAllMusic() {
      if (!this.isMusicListNow) {
        this.$store.commit('changeMusicList', this.musicList);
      }
      this.playNewMusic(this.musicList[0].songId, this.getType(this.musicList[0].type));
    },
    removeMusicList(index) {
      this.$store.commit('removeMusicList', index);
      if (this.editing) {
        this.editInfo.songs.splice(index, 1);
      }
    },
    getMusicListById(id) {
      return APIURL.GETMENUBYID(id)
        .then((res) => {
          const data = res.data;
          const menu = data.menu;
          this.connect = true;
          if (data.success) {
            this.status = true;
            if (menu) {
              this.musicMenu.menuname = menu.menuname;
              this.musicMenu.intro = menu.intro;
              this.musicMenu.songs = menu.songs;
            }
          } else {
            this.status = false;
            this.$router.replace('/404');
          }
        })
        .catch(() => {
          this.connect = false;
          this.status = false;
        });
    },
    createMenu() {
      this.hideHint();
      if (!this.editInfo.menuname) {
        this.showHint('请输入歌单名');
        return;
      }
      if (!this.editInfo.intro) {
        this.showHint('请输入歌单简介');
        return;
      }

      APIURL.ADDMENU(
        this.token,
        JSON.stringify({
          menuname: this.editInfo.menuname,
          intro: this.editInfo.intro,
          creator: this.uid,
          songs: this.musicList,
        }),
      )
        .then((res) => {
          const data = res.data;
          if (data.success) {
            this.$router.push({
              name: 'MusicList',
              params: {
                id: data.data.mid,
              },
            });
          } else {
            this.showHint('歌单创建失败');
          }
        })
        .catch(() => {
          this.showHint('服务器连接失败');
        });
    },
    init() {
      this.connect = false;
      this.status = true;
      this.editing = false;
      this.musicMenu = {
        menuname: '...',
        intro: '...',
        songs: [],
      };
      if (!this.isMusicListNow) {
        this.getMusicListById(this.$route.params.id);
      }
    },
  },
  mounted() {
    this.init();
  },
};
</script>
<style lang="scss">
@import '../scss/theme.scss';
.musiclist-header {
  display: flex;
  align-items: flex-end;
  margin-bottom: 25px;
  .musiclist-title {
    margin-left: 50px;
    font-size: 2.8rem;
  }
  .musiclist-controller {
    display: flex;
  }
  .musiclist-controller-btn {
    cursor: pointer;
    font-size: 1.4rem;
    margin-left: 20px;
  }
}

.musiclist-intro {
  margin-left: 50px;
  margin-bottom: 10px;
  font-size: 1.3rem;
  color: #aaaaaa;
}

.musiclist-content {
  .musiclist-empty {
    text-align: center;
    font-size: 2rem;
  }
  .musiclist-li {
    border-bottom: 1px solid #999999;
    display: flex;
    flex-direction: row;
    justify-content: space-between;
    transition: all 0.3s ease;
    &:hover {
      background-color: #fafafa;
    }
    .li-info {
      margin-left: 20px;
      margin-top: 20px;
      margin-bottom: 20px;
      .li-title {
        font-size: 1.6rem;
        color: $color_accent;
      }
      .li-singer {
        font-size: 1.2rem;
        margin-left: 20px;
      }
      .li-intro {
        margin-top: 15px;
        font-size: 1.3rem;
        color: #aaaaaa;
      }
    }
    .li-controller {
      display: flex;
      flex-direction: row;
      justify-content: flex-end;
      align-items: center;
      font-size: 1.2rem;
      .li-controller-btn {
        cursor: pointer;
        margin-right: 20px;
      }
    }
  }
}

.musiclist-edit {
  .form-group {
    margin-top: 10px;
    margin-bottom: 0;
    input {
      width: 400px;
      &::-webkit-input-placeholder {
        color: #bababa;
      }
    }
  }
}

.create-hint {
  font-size: 1.3rem;
  text-align: center;
  margin-top: 20px;
  color: red;
}

.create-btn {
  margin: auto;
  cursor: pointer;
  margin-top: 20px;
  width: 120px;
  height: 40px;
  border: 2px solid $color_primary;
  text-align: center;
  line-height: 40px;
  font-size: 1.5rem;
}
</style>
